// =============================================================
// CTripleSpaces
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Oct. 9, 2007
// =============================================================
#include "CTripleSpaces.h"

CTripleSpaces::CTripleSpaces(char *dip, int dport)
{
	host = dip;
	port = dport;

	tspace = "";
	wspace = NULL;
}

CTripleSpaces::~CTripleSpaces()
{
	close();
}

int CTripleSpaces::open(char *ts)
{
	tspace = ts;

	if(wspace==NULL) {
		wspace = new CWorkSpaces((char *)host.c_str(), port, CUBE_SERVICE_SPACE, (char *)tspace.c_str());
		wspace->autoclean(false);
	}
}

int CTripleSpaces::close()
{
	if(wspace!=NULL) delete wspace;
}

int CTripleSpaces::write(char *row, char *column, char *data)
{
	string srow = row;
	string scolumn = column;
	string sdata = data;

	pair<string, string> kv;
	kv.first = srow + ":" + scolumn;
	kv.second = sdata;

	return wspace->write(srow, kv);
}

int CTripleSpaces::write(string &row, string &column, string &data)
{
	pair<string, string> kv;
	kv.first = row + ":" + column;
	kv.second = data;

	return wspace->write(row, kv);
}

int CTripleSpaces::read(string &row, string &column, string &data)
{
	pair<string, string> kv;
	kv.first = row + ":" + column;
	kv.second = data;

	int status = wspace->readIfExists(row, kv);

	column = kv.first.substr(kv.first.find(":")+1);
	data = kv.second;

	return status;
}

int CTripleSpaces::take(string &row, string &column, string &data)
{
	pair<string, string> kv;
	kv.first = row + ":" + column;
	kv.second = data;

	int status = wspace->takeIfExists(row, kv);

	column = kv.first.substr(kv.first.find(":")+1);
	data = kv.second;

	return status;
}
